package cn.attackme.myuploader.controller;

import cn.attackme.myuploader.service.FileService;
import cn.attackme.myuploader.service.FileUploadService;
import cn.attackme.myuploader.service.SectionUploadService;
import com.qcloud.cos.model.ObjectListing;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 大文件上传
 */
@RestController
@CrossOrigin
public class BigFileUploadController {
    @Autowired
    private FileService fileService;

    @Autowired
    private FileUploadService fileUploadService;
    @Autowired
    private SectionUploadService sectionUploadService;

    @RequestMapping("/BigFile")
    @ResponseBody
    void upload(
            @RequestParam("name") String name,
            @RequestParam("md5") String md5,
            @RequestParam("size") Long size,
            @RequestParam("chunks") Integer chunks,
            @RequestParam("chunk") Integer chunk,
            @RequestPart("file") MultipartFile file)throws IOException {
        System.out.println(name + " " + md5 + " " + file);
        if (chunks != null && chunks != 0) {
            fileService.uploadWithBlock(name, md5,size,chunks,chunk,file);
        } else {
         fileService.upload(name, md5, file);
          //fileUploadService.upload(file);

        }
    }


    @RequestMapping("/FileToCos")
    @ResponseBody
    public void upload(
            String name,
            String md5,
 //           Long size,
//                       Integer chunks,
//                       Integer chunk,
            String path
       //     MultipartFile file1
    ) throws IOException {
        //  System.out.println(name + " " + md5 + " " + file);
//        if (chunks != null && chunks != 0) {
//            fileService.uploadWithBlock(name, md5,size,chunks,chunk,file);
//        } else {
        // fileService.upload(name, md5, file);
       // String path = "G:/map_conversion/0717/0710/_alllayers";
        sectionUploadService.upload(path);

//        }
    }



    @RequestMapping("/selectCosFile")
    @ResponseBody
    public ObjectListing selectCosFile(String path){
        return fileUploadService.getList(path);
    }

////    @RequestMapping("/BigFile")
}
